﻿<!-- 正文开始 -->
<style >
     .layui-table-cell{
         height: auto!important;
         white-space: normal;
     }
</style>

<!-- 正文开始 -->
<div class="layui-fluid"">
	<div class="layui-card">
	    <div class="layui-card-body" >
	    	<div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn" id="upload-picture-btn">
                            <i class="layui-icon">&#xe681;</i>上传
                        </button>
                    </div>
                </div>
            </div>
	        <table class="layui-table" id="tablepicture" lay-filter="tablepicture"></table>
	    </div>
	</div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="tableBarpicture">
    <a style="padding-top: 2px;height: 25px;margin-bottom: 2px;" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- js部分 -->
<script>
layui.use(['layer', 'form', 'table', 'util', 'admin','upload'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        var config = layui.config;
        var upload = layui.upload;
        var cabId = '#(cabId)';
        
        var insTb = table.render({
            elem: '#tablepicture',
            url: '#(ctx)/biz/cab/picture/listData',
            page: true,
            where: {cabId: cabId},
            cols: [[
                {type: 'numbers'},
                {
                    align: 'center', templet: function (d) {
                    	if(d.url)
                        	return '<img src="#(ctx)'+d.url+'" />';
                        else
                        	return '';
                    }, title: '缩略图', width: 100, unresize: true
                },
                {field: 'name', sort: false, title: '名称'},
                {field: 'width', sort: false, title: '宽度'},
                {field: 'height', sort: false, title: '高度'},
                {align: 'center', toolbar: '#tableBarpicture',width:120, title: '操作'}
            ]]
        });

		upload.render({
      		elem: '#upload-picture-btn',
      		url: '#(ctx)/biz/cab/picture/upload',
      		accept: 'images',
      		acceptMime: 'image/*',
      		data: {
    			cabId: cabId
    		},
      		done: function(res, index, upload) {
      			layer.closeAll('loading'); //关闭loading
      			if (200 == res.code) {
      				layer.msg(res.message, {
      					picture: 1
      				});
      				insTb.reload();
      			} else {
      				layer.msg(res.message, {
      					picture: 2
      				});
      			}
      		},
      		error: function(index, upload) {
      			layer.closeAll('loading');
      			layer.msg("上传失败！", {
      				picture: 2
      			});
      		}
      	});
        // 工具条点击事件
        table.on('tool(tablepicture)', function (obj) {	
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'del') { // 删除
                doDel(obj);
            }
        });
       	
        // 删除
        function doDel(obj) {
            layer.confirm('确定要删除“【<span style="color:red;">' + obj.data.name + '</span>】”吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('biz/cab/picture/delete', {
                    id: obj.data.id
                }, function (res) {
                    layer.closeAll('loading');
                    if (200==res.code) {
                        layer.msg(res.message, {picture: 1});
                        insTb.reload();
                    } else {
                        layer.msg(res.message, {picture: 2});
                    }
                }, 'get');
            });
        }
        
    });
</script>